package com.lxq.LeetCode.intToRoman;

import java.util.HashMap;

public class Solution {
    public static void main(String[] args) {
        String res = intToRoman(3999);
        System.out.println(res);
    }

    public static String intToRoman(int num) {
        String res = "";
        HashMap<Integer, String> matchMap = new HashMap<>();
        matchMap.put(1, "I");
        matchMap.put(5, "V");
        matchMap.put(10, "X");
        matchMap.put(50, "L");
        matchMap.put(100, "C");
        matchMap.put(500, "D");
        matchMap.put(1000, "M");
        matchMap.put(4, "IV");
        matchMap.put(9, "IX");
        matchMap.put(40, "XL");
        matchMap.put(90, "XC");
        matchMap.put(400, "CD");
        matchMap.put(900, "CM");

        int digits = 1;
        while (num != 0) {
            String tempRoman = "";
            int remainder = (num % 10);
            if (remainder == 4 || remainder == 5 ||remainder == 9) {
                tempRoman += matchMap.get(remainder * digits);
            } else {
                if (remainder > 5) {
                    tempRoman += matchMap.get(5 * digits);
                    remainder -= 5;
                }
                for (int i = remainder; i > 0; i--) {
                    tempRoman += matchMap.get(digits);
                }
            }

            res = tempRoman + res;

            num = num / 10;
            digits = digits * 10;
        }


        return res;
    }
}
